package com.jt.zxchoujiang.service.impl;

import com.jt.zxchoujiang.mapper.ChouJiangMapper;
import com.jt.zxchoujiang.pojo.ChouJiang;
import com.jt.zxchoujiang.pojo.Prize;
import com.jt.zxchoujiang.service.IChouJiangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class ChouJiangServiceImpl implements IChouJiangService {

    @Autowired(required = false)
    private ChouJiangMapper chouJiangMapper;

    @Override
    public String add(ChouJiang choujiang) {
        List<Prize> list = chouJiangMapper.findAllPrize();
        Prize[] contents = new Prize[list.size()];
        for(int i=0;i<contents.length;i++){
            contents[i] = list.get(i);
        }
        int index = new Random().nextInt(contents.length);
        String content = contents[index].getContent();
        choujiang.setResult(content);
        int row = chouJiangMapper.insert(choujiang);
        if(row==1){
            return content;
        }
        return "抽奖失败";
    }

    @Override
    public List<ChouJiang> findAll() {
        return chouJiangMapper.findAll();
    }

    @Override
    public int addPrize(Prize prize) {
        return chouJiangMapper.insertPrize(prize);
    }

    @Override
    public List<Prize> findAllPrize() {
        return chouJiangMapper.findAllPrize();
    }
}
